home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtoico.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  2.9 KB  |  136 lines

  1. /* pbmtoicon.c - read a portable bitmap and produce a Sun icon file
  2. **
  3. ** Copyright (C) 1988 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14.  
  15. static void putinit ARGS(( void ));
  16. static void putbit ARGS(( bit b ));
  17. static void putrest ARGS(( void ));
  18. static void putitem ARGS(( void ));
  19.  
  20. void
  21. main( argc, argv )
  22.     int argc;
  23.     char* argv[];
  24.     {
  25.     FILE* ifp;
  26.     bit* bitrow;
  27.     register bit* bP;
  28.     int rows, cols, format, pad, padleft, padright, row, col;
  29.  
  30.     pbm_init( &argc, argv );
  31.  
  32.     if ( argc > 2 )
  33.     pm_usage( "[pbmfile]" );
  34.  
  35.     if ( argc == 2 )
  36.     ifp = pm_openr( argv[1] );
  37.     else
  38.     ifp = stdin;
  39.  
  40.     pbm_readpbminit( ifp, &cols, &rows, &format );
  41.     bitrow = pbm_allocrow( cols );
  42.     
  43.     /* Round cols up to the nearest multiple of 16. */
  44.     pad = ( ( cols + 15 ) / 16 ) * 16 - cols;
  45.     padleft = pad / 2;
  46.     padright = pad - padleft;
  47.  
  48.     fprintf (stdout,  "/* Format_version=1, Width=%d, Height=%d", cols + pad, rows );
  49.     fprintf (stdout,  ", Depth=1, Valid_bits_per_item=16\n */\n" );
  50.  
  51.     putinit( );
  52.     for ( row = 0; row < rows; ++row )
  53.     {
  54.     pbm_readpbmrow( ifp, bitrow, cols, format );
  55.     for ( col = 0; col < padleft; ++col )
  56.         putbit( 0 );
  57.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  58.         putbit( *bP );
  59.     for ( col = 0; col < padright; ++col )
  60.         putbit( 0 );
  61.         }
  62.  
  63.     pm_close( ifp );
  64.  
  65.     putrest( );
  66.  
  67.     pm_close (stdout);
  68.  
  69.     exit( 0 );
  70.     }
  71.  
  72. static int item, bitsperitem, bitshift, itemsperline, firstitem;
  73.  
  74. static void
  75. putinit( )
  76.     {
  77.     itemsperline = 0;
  78.     bitsperitem = 0;
  79.     item = 0;
  80.     bitshift = 15;
  81.     firstitem = 1;
  82.     }
  83.  
  84. #if __STDC__
  85. static void
  86. putbit( bit b )
  87. #else /*__STDC__*/
  88. static void
  89. putbit( b )
  90. bit b;
  91. #endif /*__STDC__*/
  92.     {
  93.     if ( bitsperitem == 16 )
  94.     putitem( );
  95.     ++bitsperitem;
  96.     if ( b == PBM_BLACK )
  97.     item += 1 << bitshift;
  98.     --bitshift;
  99.     }
  100.  
  101. static void
  102. putrest( )
  103.     {
  104.     if ( bitsperitem > 0 )
  105.     putitem( );
  106.     putchar( '\n' );
  107.     }
  108.  
  109. static void
  110. putitem( )
  111.     {
  112.     char* hexits = "0123456789abcdef";
  113.  
  114.     if ( firstitem )
  115.     firstitem = 0;
  116.     else
  117.     putchar( ',' );
  118.     if ( itemsperline == 8 )
  119.     {
  120.     putchar( '\n' );
  121.     itemsperline = 0;
  122.     }
  123.     if ( itemsperline == 0 )
  124.     putchar( '\t' );
  125.     putchar( '0' );
  126.     putchar( 'x' );
  127.     putchar( hexits[item >> 12] );
  128.     putchar( hexits[( item >> 8 ) & 15] );
  129.     putchar( hexits[( item >> 4 ) & 15] );
  130.     putchar( hexits[item & 15] );
  131.     ++itemsperline;
  132.     bitsperitem = 0;
  133.     item = 0;
  134.     bitshift = 15;
  135.     }
  136.